<template>
  <div class="add">
    <el-form ref="form" :model="form" label-width="8rem">
      <el-form-item label="姓名">
        <el-input v-model="form.trueName" placeholder="请输入姓名" style="width: 60%"></el-input>
      </el-form-item>
      <el-form-item label="职工号">
        <el-input v-model="form.userNumber" placeholder="请输入职工号" style="width: 60%"></el-input>
      </el-form-item>
      <el-form-item label="电话号码">
        <el-input v-model="form.telephone" placeholder="请输入电话号码" style="width: 60%"></el-input>
      </el-form-item>
      <el-form-item label="QQ">
        <el-input v-model="form.oicq" placeholder="请输入QQ" style="width: 60%"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.mail" placeholder="请输入邮箱" style="width: 60%"></el-input>
      </el-form-item>
      <el-form-item label="用户名">
        <el-input v-model="form.loginName" placeholder="请输入用户名" style="width: 60%"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.loginPass" placeholder="请输入密码" style="width: 60%"></el-input>
      </el-form-item>
      <el-form-item label="确认密码">
        <el-input v-model="form.confirmPass" placeholder="请再次输入密码" style="width: 60%"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="addData()">创建新用户</el-button>
        <el-button @click="$router.back()">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import $ from "@/api/sysuser";

export default {
  data() {
    return {
      form: {
        loginName: "",
        loginPass: "",
        confirmPass: "",
        depId: "",
        telephone: "",
        userNumber: "",
        trueName: "",
        roleId: "003",
        oicq: "",
        mail: "",
        roleIdList: [],
        departmentList: []
      },
      options: []
    };
  },
  created() {
    $.getRoleList().then(response => {
      if (response.success) {
        // console.log(response.data)
        this.form.roleIdList = response.data.list;
      }
    });
  },
  methods: {
    addData() {
      if (!this.validate()) return;
      $.addUser(this.form).then(response => {
        if (response.success) {
          if (response.data === 0) {
            this.$message({
              message: "用户名或手机号或学号重复"
            });

          } else {
            this.$router.replace("index");
          }
        }
      });
    },
    validate() {
      let error = "";
      if (this.form.trueName.length <= 1) {
        error = "姓名至少两位\n";
      } else if (this.form.loginPass.length == 0) {
        error = "密码不能为空\n";
      } else if (this.form.confirmPass.length == 0) {
        error = "确认密码错误\n";
      } else if (this.form.loginPass != this.form.confirmPass) {
        error = "确认密码错误\n";
      } else if (!/^1\d{10}$/.test(this.form.telephone)) {
        error = "请输入正确的手机号码\n";
      } else if (!/^[a-zA-Z0-9._-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(this.form.mail)) {
        error = "请输入正确的邮箱\n";
      } else if (this.form.userNumber.length == 0) {
        error = "学/职工号不能为空\n";
      } else if (this.form.loginName.length == 0) {
        error = "用户名不能为空\n";
      } else if (this.form.roleId.length == 0) {
        error = "请赋予权限\n";
      }

      if (error) {
        this.$message({
          message: error,
          type: "error"
        });
        return false;
      }
      return true;
    }
  }
};
</script>
<style scoped>
.add {
  padding: 1rem;
}
</style>
